package com.yidu.web.dao;


import com.yidu.web.model.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: LoggerDao
 * @Description: 消息dao
 * @author: GXY
 * @date: 2020/2/27 17:24
 */
public class LoggerDao {

    public int insert(Logger logger) {
        Connection con = null;
        PreparedStatement statement = null;

        try {
            con = JDBCTools.getCon();
            String sql = "insert into ws_logger values (null, ?, ?, ?, ?, ?, ?, ?)";
            statement = con.prepareStatement(sql);
            statement.setInt(1, logger.getUserSendId());
            statement.setString(2, logger.getSendName());
            statement.setInt(3, logger.getUserAcceptId());
            statement.setString(4, logger.getAcceptName());
            statement.setString(5, logger.getLoggerMessage());
            statement.setTimestamp(6, logger.getLoggerTime());
            statement.setInt(7, logger.getLoggerState());
            return statement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(con, statement);
        }
        return 0;
    }


    public int delete(Integer loggerId) {
        Connection con = null;
        PreparedStatement statement = null;

        try {
            con = JDBCTools.getCon();
            String sql = "delete from ws_logger where logger_id = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1, loggerId);
            return statement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(con, statement);
        }
        return 0;
    }

    public int update(Logger logger) {
        Connection con = null;
        PreparedStatement statement = null;

        try {
            con = JDBCTools.getCon();
            String sql = "update ws_logger set user_send_id = ?, send_name = ?, user_accept_id = ?, accept_name = ?, logger_message = ?, logger_time = ?, logger_state = ? where logger_id = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1, logger.getUserSendId());
            statement.setString(2, logger.getSendName());
            statement.setInt(3, logger.getUserAcceptId());
            statement.setString(4, logger.getAcceptName());
            statement.setString(5, logger.getLoggerMessage());
            statement.setTimestamp(6, logger.getLoggerTime());
            statement.setInt(7, logger.getLoggerState());
            statement.setInt(8, logger.getLoggerId());
            return statement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(con, statement);
        }
        return 0;
    }


    public Logger select(Integer loggerId) {
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCTools.getCon();
            String sql = "select * from ws_logger where logger_id = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1, loggerId);
            rs = statement.executeQuery();
            while (rs.next()) {
                Logger logger = new Logger();
                logger.setLoggerId(rs.getInt(1));
                logger.setUserSendId(rs.getInt(2));
                logger.setSendName(rs.getString(3));
                logger.setUserAcceptId(rs.getInt(4));
                logger.setAcceptName(rs.getString(5));
                logger.setLoggerMessage(rs.getString(6));
                logger.setLoggerTime(rs.getTimestamp(7));
                logger.setLoggerState(rs.getInt(8));
                return logger;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(con, statement, rs);
        }
        return null;
    }



    public List<Logger> selectAll() {
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        List<Logger> list = new ArrayList<Logger>();
        try {
            con = JDBCTools.getCon();
            String sql = "select * from ws_logger";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()) {
                Logger logger = new Logger();
                logger.setLoggerId(rs.getInt(1));
                logger.setUserSendId(rs.getInt(2));
                logger.setSendName(rs.getString(3));
                logger.setUserAcceptId(rs.getInt(4));
                logger.setAcceptName(rs.getString(5));
                logger.setLoggerMessage(rs.getString(6));
                logger.setLoggerTime(rs.getTimestamp(7));
                logger.setLoggerState(rs.getInt(8));
                list.add(logger);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(con, statement, rs);
        }
        return list;
    }


    /**
     * 根据对话双方的用户ID查询聊天记录
     * @param userIdForm
     * @param userIdTo
     * @return
     */
    public List<Logger> selectAll(Integer userIdForm, Integer userIdTo) {
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        List<Logger> list = new ArrayList<Logger>();
        try {
            con = JDBCTools.getCon();
            String sql = "select * from ws_logger where (user_send_id = ? and user_accept_id = ?) " +
                            "  or (user_send_id = ? and user_accept_id = ?) order by logger_time ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,userIdForm);
            statement.setInt(2,userIdTo);

            statement.setInt(3,userIdTo);
            statement.setInt(4,userIdForm);

            rs = statement.executeQuery();
            while (rs.next()) {
                Logger logger = new Logger();
                logger.setLoggerId(rs.getInt(1));
                logger.setUserSendId(rs.getInt(2));
                logger.setSendName(rs.getString(3));
                logger.setUserAcceptId(rs.getInt(4));
                logger.setAcceptName(rs.getString(5));
                String mess = null;
                try {
                    mess = URLDecoder.decode(rs.getString(6),"UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                logger.setLoggerMessage(mess);
                logger.setLoggerTime(rs.getTimestamp(7));
                logger.setLoggerState(rs.getInt(8));
                list.add(logger);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(con, statement, rs);
        }
        return list;
    }



    public List<Logger> selectAll(String userName, String userName1) {
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        List<Logger> list = new ArrayList<Logger>();
        try {
            con = JDBCTools.getCon();
            String sql = "select * from ws_logger where (send_name = "+userName+" and accept_name = "+userName1+") " +
                    "  or (send_name = "+userName1+" and accept_name = "+userName+")";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()) {
                Logger logger = new Logger();
                logger.setLoggerId(rs.getInt(1));
                logger.setUserSendId(rs.getInt(2));
                logger.setSendName(rs.getString(3));
                logger.setUserAcceptId(rs.getInt(4));
                logger.setAcceptName(rs.getString(5));
                logger.setLoggerMessage(rs.getString(6));
                logger.setLoggerTime(rs.getTimestamp(7));
                logger.setLoggerState(rs.getInt(8));
                list.add(logger);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(con, statement, rs);
        }
        return list;
    }
}
